package cn.ghx.tool.novel;

import cn.ghx.tool.novel.grabber.Grabber;
import cn.ghx.tool.novel.grabber.GrabberFactory;

/**
 * 主入口
 */
public class Application {

    public static void main(String[] args) {
        welcome();

        String url = null;
        String path = null;

        int i=0;
        for(String p:args){
            if(p.equalsIgnoreCase("-l")){
                try {
                    url = args[i+1];
                } catch (Exception e) {
                    //e.printStackTrace();
                    log("请指定抓取的小说链接地址");
                }
            }else if(p.equalsIgnoreCase("-p")){
                try {
                    path = args[i+1];
                } catch (Exception e) {
                    //e.printStackTrace();
                    log("请指定抓取的小说txt文件存放根目录");
                }
            }

            i++;
        }
        if(url == null){
            log("请指定抓取的小说链接地址");
        }else{
            path = path != null ? path : "";
            try {
                Grabber grabber = GrabberFactory.create(url,path);
                grabber.parse();
            } catch (Exception e) {
                //e.printStackTrace();
                log(e.getMessage());
            }

        }
    }

    private static void welcome() {
        String[] site = new String[]{
                "http://www.biquku.com",
                "http://www.xinguli.com",
        };
        log("###############################################");
        log("欢迎使用小说抓取工具\n作者:guohengxi.dennis@gmail.com\n反馈:https://gitee.com/ghx/biqu/issues\n");
        log("目前支持的网站如下：");
        for (int i=1;i<=site.length;i++){
            log(i+". "+site[i-1]);
        }
        log("###############################################\n");
    }

    private static void log(String msg) {
        System.out.println(msg);
    }


}
